import mongoose, { Document, Schema } from 'mongoose';

export interface IActivity extends Document {
  user: mongoose.Types.ObjectId;
  type: 'create' | 'update' | 'delete' | 'publish' | 'archive' | 'view' | 'like' | 'comment';
  target: string; // 目标类型：lessonPlan, comment等
  targetId: mongoose.Types.ObjectId; // 目标ID
  targetTitle?: string; // 目标标题（用于教案等）
  content: string; // 活动描述
  metadata?: any; // 额外的元数据
  createdAt: Date;
}

const activitySchema = new Schema<IActivity>({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  type: {
    type: String,
    enum: ['create', 'update', 'delete', 'publish', 'archive', 'view', 'like', 'comment'],
    required: true
  },
  target: {
    type: String,
    required: true
  },
  targetId: {
    type: Schema.Types.ObjectId,
    required: true
  },
  targetTitle: {
    type: String
  },
  content: {
    type: String,
    required: true
  },
  metadata: {
    type: Schema.Types.Mixed
  }
}, {
  timestamps: { createdAt: true, updatedAt: false }
});

// 创建索引
activitySchema.index({ user: 1, createdAt: -1 });
activitySchema.index({ targetId: 1, type: 1 });
activitySchema.index({ createdAt: -1 });

export const Activity = mongoose.model<IActivity>('Activity', activitySchema);